package com.tutu.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tutu.reggie.common.BaseContext;
import com.tutu.reggie.common.R;
import com.tutu.reggie.entity.ShoppingCart;
import com.tutu.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加菜品或套餐到购物车
     *TODO 存在逻辑问题  可以优化
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("添加到购物车的数据:{}", shoppingCart);
        //设置用户id  获取当前登录用户的id
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);
        shoppingCart.setUserId(currentId);
        //判断当前添加到购物车的是菜品还是购物车
        if (shoppingCart.getDishId() != null) {
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询当前菜品或套餐是否已经在购物车中
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if (ObjectUtils.isEmpty(one)) {
            //若不存在 向购物车中添加数据 数量默认为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        } else {
            //若已经存在  数量number加1即可
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            one.setCreateTime(LocalDateTime.now());
            shoppingCartService.updateById(one);
        }
        return R.success(one);
    }

    /**
     * 减去购物车中的菜品或套餐
     * @param shoppingCart
     * @return
     */
    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("减去的菜品或套餐id:{}",shoppingCart);
        //减一的为菜品
        if(shoppingCart.getDishId()!=null){
            shoppingCartService.updateDish(shoppingCart.getDishId());
        }
        //减一的为套餐
        if (shoppingCart.getSetmealId()!=null){
            shoppingCartService.updateSetmeal(shoppingCart.getSetmealId());
        }
        return R.success("减1成功");
    }

    /**
     * 查看购物车
     * @return  返回购物车中的数据
     */
    @GetMapping("/list")
    public  R<List<ShoppingCart>> list(){
        //获得登录的用户id
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 根据用户id清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(queryWrapper);
        return R.success("成功清空购物车!");
    }


}
